package wifi.source;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

/**
 * 
 * @author George Oprina
 * 
 *         08.06.2011
 */

public class WiFiCon extends Activity {

	// the access point name to connect to
	private String ssid = "";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		try {
			// inflate the view for this activity
			this.setContentView(R.layout.set_server);

			// edit parameters
			final TextView crt_ap_name = (TextView) findViewById(R.id.tv_addr);
			crt_ap_name.setText("No AP!");

			final EditText editText = (EditText) findViewById(R.id.set_address);
			editText.setText("AP Name");

			final Button b = (Button) findViewById(R.id.set_button);

			b.setOnClickListener(new OnClickListener() {

				// manage button actions
				@Override
				public void onClick(View arg0) {

					// on click set access point name
					if (!"AP Name".equals(editText.getText().toString()
							.replaceAll("[\\n\\s]", ""))) {
						WiFiCon.this.ssid = editText.getText().toString()
								.replaceAll("[\\n\\s]", "");
						Functions.makeToast("New AP : "
								+ editText.getText().toString(), WiFiCon.this,
								0);
					}
					InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
					imm.hideSoftInputFromWindow(b.getApplicationWindowToken(),
							0);

					crt_ap_name.setText(WiFiCon.this.ssid);
				}
			});

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// create menu
		menu.add(0, 1, Menu.NONE, "Scan Wifi");
		menu.add(0, 2, Menu.NONE, "Connect");
		menu.add(0, 3, Menu.NONE, "Get Page");
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		// handle menu events
		super.onOptionsItemSelected(item);
		switch (item.getItemId()) {
		case (1):
			Intent enableWifi = new Intent();
			enableWifi.setClass(this, ScanWifi.class);
			startActivity(enableWifi);
			break;
		case (2):
			if (!ssid.equals("")) {
				// connect to my access point
				// edit this to connect to other access points
				WifiConfiguration wifi_conf = new WifiConfiguration();
				wifi_conf.SSID = ssid;
				wifi_conf.preSharedKey = "oprina_george";
				wifi_conf.networkId = 50;

				WifiManager wifi_manager = (WifiManager) this
						.getSystemService(WIFI_SERVICE);

				/*
				 * 
				 * // disable wifi (just in case)
				 * 
				 * Functions.makeToast("Disabled Wi-Fi", this, 1);
				 * 
				 * wifi_manager.setWifiEnabled(false); while
				 * (wifi_manager.isWifiEnabled()) { Functions.sleep(300); }
				 */

				// enable wifi
				if (!wifi_manager.isWifiEnabled()) {
					wifi_manager.setWifiEnabled(true);
					while (!wifi_manager.isWifiEnabled()) {
						Functions.sleep(300);
					}
					Functions.makeToast("Enabled Wi-Fi", this, 1);
				}

				// connect to AP
				wifi_manager.addNetwork(wifi_conf);
				wifi_manager.enableNetwork(50, true);

				while (wifi_manager.getConnectionInfo().getSSID() == null)
					Functions.sleep(300);

				Functions.makeToast("Connected to "
						+ wifi_manager.getConnectionInfo().getSSID(), this);
			} else
				Functions.makeToast("NO AP!!!", this, 1);
			break;
		case (3):
			// start new activity to get page
			Intent getPage = new Intent();
			getPage.setClass(this, GetPage.class);
			startActivity(getPage);
			break;
		}

		return true;
	}
}